var port = chrome.extension.connect();

port.onMessage.addListener(function(msg) {
    console.debug("receive command: " + msg.cmd + ', url: ' + msg.url + ', title: ' + msg.title);
    
    if (msg.cmd == "getSelection")
    {
        var range = window.getSelection().getRangeAt(0);
        var text = range ? range.toString() : '';
        
        console.debug("document selection text: " + text);
            
        port.postMessage({cmd: 'getSelection', url: msg.url, title: msg.title, selection: text});
    }    
});